package com.unknowpupil.web.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.unknowpupil.web.base.BaseResult;
import com.unknowpupil.web.base.common.ResponseData;
import com.unknowpupil.web.component.annotation.commit.NotReCommit;
import com.unknowpupil.web.component.annotation.log.SysLog;
import com.unknowpupil.web.component.wallet.entity.Market;
import com.unknowpupil.web.component.wallet.service.CoinToolsService;
import com.unknowpupil.web.component.wallet.tools.CoinMarketTools;
import com.unknowpupil.web.utils.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Classname: MainConstroller
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/10/8 13:15
 * @Description:
 * @Version: 1.0
 */
@RestController
public class MainController {

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    private HttpServletRequest request;

    final static Logger logger = LoggerFactory.getLogger(MainController.class);
    @Autowired
    private CoinToolsService coinToolsService;
    @GetMapping("api/{address}")
    @ResponseBody
    public String test(@PathVariable("address") String address){
        double balance = 0;
        List<String> list = Lists.newArrayList();
        list.add(address);
        try {
            balance = coinToolsService.getBalance(address);
            logger.info("地址{}:ETH余额：{}",address,balance);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return balance+"";
    }
    @GetMapping("api/getblock")
    public String getblock(){
        double balance = 0;
        try {
            balance = coinToolsService.getBlockHeight();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return balance+"";
    }
    @GetMapping("api/parse/{height}")
    public String parseBlock(@PathVariable("height") Long height){
        double balance = 0;
        try {
            boolean b = coinToolsService.parseBlock(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }

    /**
     * base：基准币，字符串
     * quote：报价币，字符串
     * bid： 买入数量，数值
     * ask：卖出数量，数值
     * exchange：具有最大交易量的交易所名称，字符串
     * volume：交易量，数值
     * num_exchanges：查询的交易所数量
     * total_volume：查询的交易所的总交易量，数值
     * timestamp ：unix时间戳，数值
     * @return
     */
    @Autowired
    private CoinMarketTools coinMarketTools;
    @GetMapping("get/market")
    public Market get(){

        return coinMarketTools.getMarket("ETH");
    }


    @SysLog("跳转页面")
    @RequestMapping(value = {"","index"},method = RequestMethod.GET)
    public String index(Model model) throws Exception {
        String token = (String) request.getAttribute("claims_admin");
        System.out.println(token.toString());
        Map map = Maps.newHashMap();
        map.put("user","w10g");
        model.addAttribute("user",map);
        return "index";
    }


    @SysLog("增加信息")
    @PostMapping(value = "add")
    @ResponseBody
    @NotReCommit
    public ResponseData index(/*@Valid Base base,*/ HttpServletResponse response) throws Exception {

//        String token = (String) request.getAttribute("claims_user");
//        if(StringUtils.isBlank(token)){
//            throw new MxException("权限不足");
//
//        }
//        int i = 1/0;
        String admin = jwtUtil.createJWT(1 + "", "123", "admin");
//        baseService.save(base);
        return BaseResult.getInstance(response).build(400,"增加成功","","");
    }


}
